package com.ruoyi.system.service.wx.pay.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.appvo.PayAllAccount;
import com.ruoyi.system.domain.wx.pay.WxPayInfo;
import com.ruoyi.system.domain.wx.pay.WxPayType;
import com.ruoyi.system.mapper.wx.pay.WxPayInfoMapper;
import com.ruoyi.system.service.wx.pay.IWxPayInfoService;
import com.ruoyi.system.service.wx.pay.IWxPayTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 支出明细Service业务层处理
 *
 * @author mic
 * @date 2023-08-14
 */
@Service
public class WxPayInfoServiceImpl implements IWxPayInfoService {
    @Autowired
    private WxPayInfoMapper wxPayInfoMapper;
    @Autowired
    private IWxPayTypeService payTypeService;

    /**
     * 查询支出明细
     *
     * @param payId 支出明细主键
     * @return 支出明细
     */
    @Override
    public WxPayInfo selectWxPayInfoByPayId(String payId) {
        return wxPayInfoMapper.selectWxPayInfoByPayId(payId);
    }

    /**
     * 查询支出明细列表
     *
     * @param wxPayInfo 支出明细
     * @return 支出明细
     */
    @Override
    public List<WxPayInfo> selectWxPayInfoList(WxPayInfo wxPayInfo) {
        return wxPayInfoMapper.selectWxPayInfoList(wxPayInfo);
    }

    /**
     * 新增支出明细
     *
     * @param wxPayInfo 支出明细
     * @return 结果
     */
    @Override
    public int insertWxPayInfo(WxPayInfo wxPayInfo) {
        return wxPayInfoMapper.insertWxPayInfo(wxPayInfo);
    }

    /**
     * 修改支出明细
     *
     * @param wxPayInfo 支出明细
     * @return 结果
     */
    @Override
    public int updateWxPayInfo(WxPayInfo wxPayInfo) {
        return wxPayInfoMapper.updateWxPayInfo(wxPayInfo);
    }

    /**
     * 批量删除支出明细
     *
     * @param payIds 需要删除的支出明细主键
     * @return 结果
     */
    @Override
    public int deleteWxPayInfoByPayIds(String[] payIds) {
        return wxPayInfoMapper.deleteWxPayInfoByPayIds(payIds);
    }

    /**
     * 删除支出明细信息
     *
     * @param payId 支出明细主键
     * @return 结果
     */
    @Override
    public int deleteWxPayInfoByPayId(String payId) {
        return wxPayInfoMapper.deleteWxPayInfoByPayId(payId);
    }

    /**
     * 时间倒叙list
     *
     * @param limit 限制查询条数
     * @return
     */
    @Override
    public List<WxPayInfo> selectPayInfoDescDate(String userId, int limit) {
        Map<String, Object> map = new HashMap<>();
        if (StringUtils.isNotBlank(userId)) {
            map.put("userId", userId);
        }
        if (limit > 0) {
            map.put("limit", limit);
        }
        return wxPayInfoMapper.selectPayInfoDescDate(map);
    }

    /**
     * 获取年份内的数据
     *
     * @param map
     * @return
     */
    @Override
    public List<WxPayInfo> selectPayInfoListByYear(Map<String, Object> map) {
        return wxPayInfoMapper.selectPayInfoListByYear(map);
    }

    /**
     * 获取指定年月的数据
     *
     * @param map
     * @return
     */
    @Override
    public List<WxPayInfo> selectPayInfoListByMonth(Map<String, Object> map) {
        return wxPayInfoMapper.selectPayInfoListByMonth(map);
    }

    /**
     * 获取指定年月日的数据
     *
     * @param map
     * @return
     */
    @Override
    public List<WxPayInfo> selectPayInfoListByDay(Map<String, Object> map) {
        return wxPayInfoMapper.selectPayInfoListByDay(map);
    }

    /**
     * 支付明细数据汇总处理, 类型分组求和
     */
    @Override
    public List<PayAllAccount> groupTypeSum(List<WxPayInfo> list) {
        List<PayAllAccount> payAllAccounts = new ArrayList<>();
        //分组
        Map<String, List<WxPayInfo>> collect = list.stream().collect(Collectors.groupingBy(WxPayInfo::getPrimaryType));
        // 遍历分组
        for (Map.Entry<String, List<WxPayInfo>> info : collect.entrySet()) {
            PayAllAccount payAllAccount = new PayAllAccount();
            String key = info.getKey();
            // 求和
            Double sum = info.getValue().stream().mapToDouble(WxPayInfo::getPayAmount).sum();
            WxPayType wxPayType = payTypeService.selectWxPayTypeByTypeId(key);
            // 处理类型名称
            WxPayType parentType = payTypeService.selectWxPayTypeByTypeId(wxPayType.getParentId());
            payAllAccount.setName(parentType.getTypeName() + "-" + wxPayType.getTypeName());
            payAllAccount.setValue(sum + "");
            payAllAccounts.add(payAllAccount);
        }
        return payAllAccounts;
    }

    @Override
    public List<Map<String,Double>> selectInfoGroupYear(WxPayInfo payInfo) {
        return wxPayInfoMapper.selectInfoGroupYear(payInfo);
    }

    @Override
    public List<Map<String,Double>> selectInfoGroupMonth(WxPayInfo payInfo) {
        return wxPayInfoMapper.selectInfoGroupMonth(payInfo);
    }

    @Override
    public List<WxPayInfo> selectByYearList(WxPayInfo info) {
        return wxPayInfoMapper.selectByYearList(info);
    }
}
